﻿using OAZCSharpSdk.OAZ.Plugin.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace OAZCSharpSdk.OAZ.Plugin.Events
{
    public class PluginGroupMessageEventArgs : PluginEventArgs
    {

        public PluginGroupMessageEventArgs(long RobotQQ) : base(RobotQQ)
        {
        }
        /// <summary>
        /// 消息创建者
        /// </summary>
        public long QQ { get; set; }
        /// <summary>
        /// 消息创建者群名片
        /// </summary>
        public string CardName { get; set; }
        /// <summary>
        /// 群号
        /// </summary>
        public long GroupQQ { get; set; }
        /// <summary>
        /// 群名
        /// </summary>
        public string GroupName { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 安全消息，去除@代码
        /// </summary>
        public string SafeContent
        {
            get
            {
                if (_SafeContent == null)
                {
                    _SafeContent = Regex.Unescape(Regex.Replace(Content, "\\[@\\d+\\]", "").Replace("[@all]", "").TrimStart());
                }
                return _SafeContent;
            }
        }

        private string _SafeContent { get; set; }

        private string[] _SafeContentArray { get; set; }

        /// <summary>
        /// 指令，以空格分割
        /// </summary>
        public string Command
        {
            get
            {
                if (_SafeContentArray == null)
                {
                    _SafeContentArray = SafeContent.Split(' ');
                }
                return _SafeContentArray[0];
            }
        }


        public List<string> Args
        {
            get
            {
                if (_SafeContentArray == null)
                {
                    _SafeContentArray = SafeContent.Split(' ');
                }
                if (_SafeContentArray.Length == 1)
                {
                    return new List<string>();
                }
                List<string> list = _SafeContentArray.ToList();
                list.RemoveAt(0);
                return list;
            }
        }

        /// <summary>
        /// 消息创建时间
        /// </summary>
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// 是否是@全员消息
        /// </summary>
        public bool MentionEveryone
        {
            get
            {
                if (_isMentionEveryone == null)
                {
                    _isMentionEveryone = false;
                    if (Content.Contains("[@all]"))
                    {
                        if (Cache.GroupCache.IsManager(RobotQQ, GroupQQ, QQ))
                        {
                            _isMentionEveryone = true;
                        }
                    }
                }
                return (bool)_isMentionEveryone;
            }
        }

        private bool? _isMentionEveryone;
        /// <summary>
        /// 消息中@到的成员
        /// </summary>
        public List<long> Mentions
        {
            get
            {
                if (_mentions == null)
                {
                    _mentions = new List<long>();
                    foreach (Match item in Regex.Matches(Content, "\\[@(\\d+)\\]"))
                    {
                        _mentions.Add(long.Parse(item.Groups[1].Value));
                    }
                }
                return _mentions;
            }
        }

        /// <summary>
        /// 创建一条属于 <see cref="GroupQQ"/> 群的消息
        /// </summary>
        /// <returns></returns>
        public MessageContent CreateMessage()
        {
            return new MessageContent(QQ, new Action<MessageContent>((msg) =>
            {
                SendGroupMessage(GroupQQ, msg.ToString());
            }));
        }

        /// <summary>
        /// 上传群文件
        /// <param name="filePath">本地文件路径</param>
        /// <param name="toFolder">上传到哪个文件夹，填文件夹名，根目录留空或填/</param>
        /// </summary>
        public void UploadFile(string filePath,string toFolder = "/")
        {
            OAZ.Api_UpFlieGroup(RobotQQ, GroupQQ, filePath, toFolder);
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(MessageContent message)
        {
            SendMessage(message.ToString());
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(string message, bool atAuthor = false)
        {
            SendGroupMessage(GroupQQ, (atAuthor ? OAZ.At(QQ) : "") + message);
        }

        private List<long> _mentions;
    }
}
